package com.photoeditor.demo.model.bean.face;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Base64;

import com.common.base.ComponentContext;
import com.common.base.utils.FileUtil;
import com.photoeditor.demo.util.Utils;

import java.io.File;

/**
 * Created by @author winn on 2018/10/31.
 */

public class BabyPredicResultBean extends BaseFaceResultBean {

    private static final String IMG_TMP_FLODER = FileUtil.getExternalCacheDir(ComponentContext.getContext(), ".baby", false);

    /**
     * 服务器回传的小孩图片数据流（base64编码后的二进制数据）
     */
    public String pic;
    /**
     * 小孩图片保存在存储卡里的路径（保存后，需要将pic字段置空，避免Intent中传输时，数据量过大，导致Binder爆内存）
     */
    public String picFilePath;
    private int code;
    private boolean success;
    private BabyBean data;
    private String message;

    public BabyPredicResultBean() {
        super(REPORT_TYPE_BABYPREDICT);
    }

    /**
     * 将图片二进制数据保存到存储卡
     * 注意：异步操作，需要放线程
     *
     * @return 返回图片保存的路径，保存失败会返回null
     */
    public String savePic() {
        if (TextUtils.isEmpty(pic)) {
            return null;
        }
        byte[] data = Base64.decode(pic, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        String savePath = IMG_TMP_FLODER + File.separator + System.currentTimeMillis() + ".jpg";
        boolean ret = Utils.saveBitmap(bitmap, savePath);
        if (ret) {
            picFilePath = savePath;
            pic = "";
        }
        return picFilePath;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public BabyBean getData() {
        return data;
    }

    public void setData(BabyBean data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public static class BabyBean implements Parcelable {
        private String pic;
        private int sex;

        public String getPic() {
            return pic;
        }

        public void setPic(String pic) {
            this.pic = pic;
        }

        public int getSex() {
            return sex;
        }

        public void setSex(int sex) {
            this.sex = sex;
        }


        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.pic);
            dest.writeInt(this.sex);
        }

        public BabyBean() {
        }

        protected BabyBean(Parcel in) {
            this.pic = in.readString();
            this.sex = in.readInt();
        }

        public static final Creator<BabyBean> CREATOR = new Creator<BabyBean>() {
            @Override
            public BabyBean createFromParcel(Parcel source) {
                return new BabyBean(source);
            }

            @Override
            public BabyBean[] newArray(int size) {
                return new BabyBean[size];
            }
        };
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(this.pic);
        dest.writeString(this.picFilePath);
        dest.writeInt(this.code);
        dest.writeByte(this.success ? (byte) 1 : (byte) 0);
        dest.writeParcelable(this.data, flags);
        dest.writeString(this.message);
    }

    protected BabyPredicResultBean(Parcel in) {
        super(in);
        this.pic = in.readString();
        this.picFilePath = in.readString();
        this.code = in.readInt();
        this.success = in.readByte() != 0;
        this.data = in.readParcelable(BabyBean.class.getClassLoader());
        this.message = in.readString();
    }

    public static final Creator<BabyPredicResultBean> CREATOR = new Creator<BabyPredicResultBean>() {
        @Override
        public BabyPredicResultBean createFromParcel(Parcel source) {
            return new BabyPredicResultBean(source);
        }

        @Override
        public BabyPredicResultBean[] newArray(int size) {
            return new BabyPredicResultBean[size];
        }
    };
}
